package zj.com.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.ex.DbException;

import zj.com.app.ZjApplication;
import zj.com.db.DBUtil;
import zj.com.model.DzgDetailDB;
import zj.com.model.DzgInspectItemDB;
import zj.com.model.GjgxDB;
import zj.com.model.JcdInfoDB;
import zj.com.model.JcxDB;
import zj.com.model.JcxDetailDB;
import zj.com.net.NetManager;
import zj.com.net.OnResponseListener;
import zj.com.util.CommonUtil;
import zj.com.util.DialogUtil;
import zj.com.util.SharedPreferencesUtil;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/4.
 */
public class SettingActivity extends BaseActivity implements View.OnClickListener{

    public ImageView left_iv;
    public TextView title_name,right_tv,version_tv,logout_tv;
    public Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        initView();
    }

    public void initView(){
        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setVisibility(View.VISIBLE);
        left_iv.setOnClickListener(this);

        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("设置");
        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setVisibility(View.GONE);

        version_tv = (TextView) findViewById(R.id.version_tv);
        logout_tv = (TextView) findViewById(R.id.logout_tv);
        logout_tv.setOnClickListener(this);

        version_tv.setText("V"+CommonUtil.GetVersion(this));
    }

    @Override
    public void onClick(View v) {

        switch(v.getId()){
            case R.id.left_iv:
                finish();
                break;
            case R.id.logout_tv:
                logout();
                break;
        }

    }

    public ProgressDialog dialog;
    public void logout(){
        dialog = DialogUtil.createProgressDialog(this,"提示","退出处理中");
        NetManager.getInstance(this).logout(new OnResponseListener<String>() {
            @Override
            public void onComplete(boolean isOK, String result, int errorCode, String errorMsg) {



                    SharedPreferencesUtil.removeData(SettingActivity.this, "user");
                    SharedPreferencesUtil.removeData(SettingActivity.this,"token");
                    SharedPreferencesUtil.removeData(SettingActivity.this, "uid");

                    try {
                        DBUtil.getInstance(SettingActivity.this).getDBManager().delete(JcxDB.class);
                        DBUtil.getInstance(SettingActivity.this).getDBManager().delete(JcdInfoDB.class);
                        DBUtil.getInstance(SettingActivity.this).getDBManager().delete(JcxDetailDB.class);
                        DBUtil.getInstance(SettingActivity.this).getDBManager().delete(GjgxDB.class);
                        DBUtil.getInstance(SettingActivity.this).getDBManager().delete(DzgDetailDB.class);
                        DBUtil.getInstance(SettingActivity.this).getDBManager().delete(DzgInspectItemDB.class);

                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                   dialog.dismiss();
                    ZjApplication.getInstance().exit();


                    Intent intent = new Intent(SettingActivity.this,LoginActivity.class);
                    startActivity(intent);

            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                Toast.makeText(SettingActivity.this,errorMessage,Toast.LENGTH_SHORT).show();

            }
        });
    }


}
